package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.hdw.ItemCategory;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 分类管理业务切面类
 * 
 * @Date:Created in 2018/7/10 15:34
 * @Author:
 * @Modified By: zjn 2018/7/10 15:34
 */ 
@Component
@Aspect
public class ItemCategoryServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemCategoryService.find(..))")
    private void find() {}
    /**
    * 查询
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  15:44
    */ 
    @Around("find()")
    public Object find(ProceedingJoinPoint joinPoint) throws Throwable {

        Integer categoryId = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM_CATEGORY, categoryId);
        Object result = memCachedClient.get(key);
        if(result == null) {
            result = joinPoint.proceed();
            if(result != null) {
                memCachedClient.set(key, result);
            }
        }
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemCategoryService.update(..))")
    private void update() {}
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  15:44
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        ItemCategory itemCategory = (ItemCategory) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM_CATEGORY, itemCategory.getId());
        memCachedClient.delete(key);
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemCategoryService.delete(..))")
    private void delete() {}
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  15:54
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Integer categoryId = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM_CATEGORY, categoryId);
        memCachedClient.delete(key);
        return result;
    }
}
